package com.woniuxy.gobang84.ui;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import com.woniuxy.gobang84.dao.UserDAO;
import com.woniuxy.gobang84.entity.User;
import com.woniuxy.gobang84.exception.AccountExistException;
import com.woniuxy.gobang84.exception.DAOException;
import com.woniuxy.gobang84.exception.ServiceException;
import com.woniuxy.gobang84.service.UserService;
import com.woniuxy.gobang84.util.Global;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

/**
 * 注册窗口
 * 
 * @author 蜗牛学院 (<a href="http://www.woniuxy.com">蜗牛学院</a>)
 *
 */
public class RegisterStage extends Stage implements EventHandler<ActionEvent> {
	private Button btnReg = new Button("注 册");
	private Button btnCancal = new Button("取 消");
	private TextField txtAccount = new TextField();
	private PasswordField txtPassword = new PasswordField();
	private PasswordField txtCfmPwd = new PasswordField();
	private Label lblAccountMsg = new Label();
	private Label lblPwdMsg = new Label();
	private Label lblCfmPwdMsg = new Label();

	public RegisterStage() {
		setTitle("用户注册-蜗牛学院");
		setResizable(false);

		Pane pane = new Pane();
		Label nameLbl = new Label("账     号:");
		nameLbl.setPrefSize(60, 20);
		nameLbl.setLayoutX(30);
		nameLbl.setLayoutY(30);

		txtAccount.setPromptText("请输入账号");
		txtAccount.setLayoutX(100);
		txtAccount.setLayoutY(30);

		lblAccountMsg.setPrefWidth(100);
		lblAccountMsg.setLayoutX(250);
		lblAccountMsg.setLayoutY(30);

		Label pwdLbl = new Label("密      码:");
		pwdLbl.setPrefSize(60, 20);
		pwdLbl.setLayoutX(30);
		pwdLbl.setLayoutY(70);

		txtPassword.setLayoutX(100);
		txtPassword.setLayoutY(70);
		txtPassword.setPromptText("请输入密码");

		lblPwdMsg.setLayoutX(250);
		lblPwdMsg.setLayoutY(70);

		Label cfmpwdLbl = new Label("确认密码:");
		cfmpwdLbl.setPrefSize(60, 20);
		cfmpwdLbl.setLayoutX(30);
		cfmpwdLbl.setLayoutY(110);

		txtCfmPwd.setLayoutX(100);
		txtCfmPwd.setLayoutY(110);
		txtCfmPwd.setPromptText("请输入确认密码");

		lblCfmPwdMsg.setLayoutX(250);
		lblCfmPwdMsg.setLayoutY(110);

		btnReg.setLayoutX(110);
		btnReg.setLayoutY(150);
		btnCancal.setLayoutX(180);
		btnCancal.setLayoutY(150);

		btnReg.setDefaultButton(true);
		btnCancal.setCancelButton(true);

		btnReg.setOnAction(this);
		btnCancal.setOnAction(this);

		pane.getChildren().addAll(nameLbl, txtAccount, pwdLbl, txtPassword, cfmpwdLbl, txtCfmPwd, btnReg, btnCancal,
				lblAccountMsg, lblPwdMsg, lblCfmPwdMsg);

		Scene scene = new Scene(pane, 400, 180);
		setScene(scene);

	}

	@Override
	public void handle(ActionEvent event) {
		Object source = event.getSource();
		if (source == btnReg) {
			User user = new User();
			user.setAccount(txtAccount.getText());
			user.setPassword(txtPassword.getText());

			UserService userService = new UserService();
			try {
				userService.registe(user);
				
			} catch (AccountExistException e) {
				e.printStackTrace();
				Alert alert = new Alert(AlertType.ERROR);
				alert.setContentText("账号已存在");
				alert.show();
			} catch (ServiceException e) {
				Alert alert = new Alert(AlertType.ERROR);
				alert.setContentText("系统维护中,请稍后再试.....");
				alert.show();
			}
		} else {
			this.close();
		}
	}

}
